﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cash.Model;
using CashCommon;
using CashResource;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

namespace CashConnect
{
    class CashAccountGate : ICashAccountGate
    {
        public static CashAccountGate Instance
        {
            get { return new CashAccountGate(); }
        }
        
        public void RemoveCashAccount(int[] cashAccountId)
        {
            WFSql.DB.StartTransaction();
            try
            {
                SqlWork.SqlDeleteSimple("DELETE FROM dbo.FD_Suek_Cash WHERE LINK = @LINK", cashAccountId, "LINK");
                WFSql.DB.Commit();
            }
            catch
            {
                WFSql.DB.Rollback();
                throw;
            }

            
        }
        public bool CashAccountExist(int activeUserId, int cashAccountId)
        {
            Tools.ShowMessage("CashAccountGate");
            return true;
        }
        public CashAccount ApplyToDb(CashAccount cashAccount)
        {
            Tools.ShowMessage("CashAccountGate");
            return null;
        }
        public void RefreshFromDb(CashAccount cashAccount)
        {
            String sql =
                " SELECT C_Name,C_Name1,D_Date,F_Division,LINK,N_Amount,N_Code,C_Foundation,N_Tax,NameCategory,NameItem,NMB,F_Subscr,C_Order_Number," +
                " F_Sale_Categories,F_Sale_Items, N_Releaze FROM dbo.V_FD_Suek_Cash WHERE LINK = @LINK";

            SqlCommand cmd = new SqlCommand(sql, WFSql.DB.SqlConnection, WFSql.DB.SqlTransaction);
            WFSql.DB.AddInParameter("LINK", SqlDbType.BigInt, cashAccount.LINK, cmd, false);

            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.Read())
                {
                    cashAccount.Number = sdr["N_Code"].ToString();
                    cashAccount.AbonentName = sdr["C_Name1"].ToString();
                    cashAccount.DepartmentName = sdr["C_Name"].ToString();
                    cashAccount.Amount = Convert.ToDouble(sdr["N_Amount"], CultureInfo.InvariantCulture);
                    cashAccount.DatePrint = Convert.ToDateTime(sdr["D_Date"], CultureInfo.InvariantCulture);
                    cashAccount.N_Tax = Convert.ToInt32(sdr["N_Tax"], CultureInfo.InvariantCulture);
                    cashAccount.Sale_Categories_Name = sdr["NameCategory"].ToString();
                    cashAccount.Sale_Items_Name = sdr["NameItem"].ToString();
                    cashAccount.C_Foundation = sdr["C_Foundation"].ToString();
                    cashAccount.F_Subscr = Convert.ToInt32(sdr["F_Subscr"], CultureInfo.InvariantCulture);
                    cashAccount.F_Division = Convert.ToInt32(sdr["F_Division"], CultureInfo.InvariantCulture);
                    cashAccount.F_Sale_Categories = Convert.ToInt32(sdr["F_Sale_Categories"], CultureInfo.InvariantCulture);
                    cashAccount.F_Sale_Items = Convert.ToInt32(sdr["F_Sale_Items"], CultureInfo.InvariantCulture);
                    cashAccount.C_Order_Number = sdr["C_Order_Number"].ToString();
                    cashAccount.N_Releaze = Convert.ToInt32(sdr["N_Releaze"], CultureInfo.InvariantCulture);
                }
            }
        }

        public CashCommon.Sequence GetSequence(CashCommon.SqlSwitch swth)
        {
            Sequence.SqlGate = Builder.SqlGate;
            Sequence sequence = new Sequence();
            sequence.Sql = " SELECT NMB, LINK, F_Division, C_Order_Number, C_Name, N_Code, D_Date, D_Date_Search, C_Name1, N_Amount, N_Tax, NameCategory, NameItem, N_Releaze FROM dbo.V_FD_Suek_Cash WHERE 1=1";

            sequence.Fields = new List<List<string>>() {
                new List<string>() {"NMB", FieldsName.NMB}, 
				new List<string>() {"LINK", FieldsName.LINK},
				new List<string>() {"F_DIVISION", FieldsName.F_Division},
                new List<string>() {"C_NAME", FieldsName.CashAccountDivisionName},
                new List<string>() {"N_CODE", FieldsName.CashAccountCodeName},
                new List<string>() {"D_DATE", FieldsName.CashAccountDivisionDate},
                new List<string>() {"C_NAME1", FieldsName.CashAccountSubscrName},
                new List<string>() {"N_AMOUNT", FieldsName.CashAccountMoney},
                new List<string>() {"N_TAX", FieldsName.N_Tax},
                new List<string>() {"NAMECATEGORY", FieldsName.CashAccountCategoryName},
                new List<string>() {"D_DATE_SEARCH", FieldsName.CashRegisterDate112Name},
                new List<string>() {"C_ORDER_NUMBER", FieldsName.CashAccountCategoryName},
                new List<string>() {"NAMEITEM", FieldsName.CashAccountItemName},
                new List<string>() {"N_RELEAZE", FieldsName.CashAccountReleaze}
            };

            if (swth != null)
                sequence.SqlSwitch.Condition = swth.Condition.Where(i => sequence.Fields.Select(res => res[0]).Contains(i.Name.ToUpper())).ToList();

            sequence.Index = 0;
            sequence.RecordCount = Adjustment.Default.CountRecordCash;

            return sequence;
        }

    }
}
